/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.reportgear.report.model.cell.value.salary.SalaryDetailObject;
import com.reportgear.report.model.cell.value.salary.SalaryDocObject;
import com.reportgear.report.model.cell.value.salary.SalaryFormulaObject;
import com.reportgear.report.model.cell.value.salary.SalaryItemObject;
import com.reportgear.report.model.cell.value.salary.SalaryMainObject;
import com.reportgear.report.model.cell.value.salary.SalaryObject;
import com.reportgear.report.model.cell.value.salary.SalaryPayObject;
import com.sot.core.util.StringUtils;

/**
 * 模型可设计值对象管理器<br/>
 * 注册所有的可设计值
 * 
 * @version 1.0 2011-6-10
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public enum ModelDesignableValueObjectClassManager {
	INSTANCE;

	private Logger logger = Logger.getLogger(ModelDesignableValueObjectClassManager.class.getName());
	private Map<String, Class<?>> keyClassMap = new HashMap<String, Class<?>>();
	private Map<Class<?>, String> classKeyMap = new HashMap<Class<?>, String>();

	ModelDesignableValueObjectClassManager() {
		this.registerSystemObjects("S_1", String.class);
		this.registerSystemObjects("S_2", FormulaObject.class);

		this.registerSystemObjects("S_51", SalaryObject.class);
		this.registerSystemObjects("S_52", SalaryDetailObject.class);
		this.registerSystemObjects("S_53", SalaryItemObject.class);
		this.registerSystemObjects("S_54", SalaryFormulaObject.class);
		this.registerSystemObjects("S_55", SalaryDocObject.class);
		this.registerSystemObjects("S_56", SalaryMainObject.class);
		this.registerSystemObjects("S_57", SalaryPayObject.class);

		this.registerSystemObjects("S_60", JsDataFieldObject.class);
	}

	private void registerSystemObjects(String key, Class<?> clazz) {
		if (this.keyClassMap.containsKey(key)) {
			logger.warn(String.format("键值%s已经存在， 设计对象类:%s不能注册！", key, clazz));
			return;
		}

		if (this.classKeyMap.containsKey(clazz)) {
			logger.warn(String.format("类%s已经存在， 设计对象类:%s不能注册！", clazz, clazz));
			return;
		}

		this.keyClassMap.put(key, clazz);
		this.classKeyMap.put(clazz, key);
	}

	/**
	 * 获取类-键值映射
	 * 
	 * @return 类-键值映射
	 */
	public Map<Class<?>, String> getClassKeyMap() {
		return Collections.unmodifiableMap(this.classKeyMap);
	}

	/**
	 * 获取键值-类映射
	 * 
	 * @return 键值-类映射
	 */
	public Map<String, Class<?>> getKeyClassMap() {
		return Collections.unmodifiableMap(this.keyClassMap);
	}

	/**
	 * 获取类对应的键值
	 * 
	 * @param clazz
	 *            类
	 * @return 类键值
	 */
	public String getClassKey(Class<?> clazz) {
		String key = this.classKeyMap.get(clazz);

		if (StringUtils.isEmpty(key)) {
			logger.warn(String.format("获取类[%s]对应的键值不存在", clazz));
		}

		return key;
	}

	/**
	 * 获取键值对象的类
	 * 
	 * @param key
	 *            键值
	 * @return 类
	 */
	public Class<?> getKeyClass(String key) {
		Class<?> clazz = this.keyClassMap.get(key);

		if (clazz == null) {
			logger.warn(String.format("获取键值[%s]对应的类不存在", clazz));
		}

		return clazz;
	}
}
